﻿
verify = eswatTest.Services.Verify

# ---------------------------------------------------------------
#  TEST EXTERNAL .NET COMPONENT (.dll)
# ---------------------------------------------------------------

# require/include the component
import clr
clr.AddReferenceToFileAndPath("Eswat.Samples.SampleComponentLibrary")
from Eswat.Samples.SampleComponentLibrary import SampleComponent

# create new component
comp = SampleComponent()
#set and verify the title
comp.Title = "hello component world!"
verify.AreEqual("hello component world!", comp.Title, "sample component title should match")

# ---------------------------------------------------------------
#  TEST WEBSITE
# ---------------------------------------------------------------

# alias ESWAT web test service for shorter reference
web = eswatTest.Services.Web

# open IE and go to project "silk"
web.open("IE")
web.go("http://localhost:61614/Auth/SignOut") # always start at sign-out page to ensure we don't resume a previously logged-in session

# get current bg color
originalBG = web.js('return document.body.style.backgroundColor;')
# set new bg color and return it
newBG = web.js('return (document.body.style.backgroundColor = "#FF0000");')
# verify new bg color was set
verify.AreEqual("#FF0000", newBG, "bg color")
# restore original bg color
web.js("document.body.style.backgroundColor = \""+originalBG+"\"")

# verify page title
verify.AreEqual("Mileage Stats | Know where your gas takes you", web.pageTitle, "Silk landing page title should match")
# verify html.head.title
verify.AreEqual("Mileage Stats | Know where your gas takes you", web.title.text, "Silk landing title should match")

# verify <h1> text using Selenium's "By" syntax
from OpenQA.Selenium import By
verify.AreEqual("Sign In / Register", web.find(By.TagName("h1")).text, "h1 text")

# test images shortcut
verify.AreEqual(6, web.images.Count, "silk images")
# test byId shortcut
verify.IsNotNull(web.byId("pinnedSiteContainer"), "silk by id")
# test oneById shortcut
verify.IsNotNull(web.oneById("pinnedSiteContainer"), "silk one by id")
# test allByTag shortcut
verify.AreEqual(6, web.allByTag("img").Count, "silk images all by tag")
# test allByClass shortcut
verify.AreEqual(2, web.allByClass("button").Count, "silk all by class name")
# test oneByName shortcut
verify.IsNotNull(web.oneByName("providerUrl"), "silk by name")
# test attr shortcut
providerUrl = web.oneByName("providerUrl")
originalProvider = providerUrl.attr("value")
verify.AreEqual("http://oturner.myidprovider.org/", originalProvider, "silk attribute")
# test type shortcut
providerUrl.type("ironruby.net")
verify.AreEqual("ironruby.net", providerUrl.attr("value"), "silk type text")
providerUrl.type(originalProvider)
# test submit shortcut
providerUrl.submit()

# test navigation result (next page)
verify.AreEqual("Mock Authentication", web.pageTitle, "Silk login title should match")
web.byId("claimed_identifier").submit()

# test another navigation result (next page)
verify.AreEqual("Dashboard", web.pageTitle, "Silk login title should match")
verify.AreEqual("Sample User", web.byId("welcome").oneByTag("span").text, "welcome name");
nav = web.oneByClass("nav").allByTag("a")
# test properties of all...() shortcut result
verify.AreEqual(4, nav.Count, "nav count")
verify.AreEqual("Dashboard", nav[0].text, "nav dashboard")
verify.AreEqual("Charts", nav[1].text, "nav charts")
verify.AreEqual("Profile", nav[2].text, "nav profile")
verify.AreEqual("Sign Out", nav[3].text, "nav sign out")
# test oneByPartialLinkText shortcut (actual link text is "+ Add Vehicle"
verify.IsNotNull(web.oneByPartialLinkText("Add Vehicle"), "oneByPartialLinkText")
# test oneByLinkText shortcut
verify.IsNotNull(web.oneByLinkText("+ Add Vehicle"), "oneByLinkText")

web.go("http://localhost:61614/Auth/SignOut")

# quit (close IE)
web.quit()

# ---------------------------------------------------------------
#  TEST "verify" API
# ---------------------------------------------------------------

verify.True(True)
verify.True(True, "True(True) should succeed")
verify.True(True, "True({0}) should succeed", "True")
verify.True(False, "True(False) should fail")

verify.IsTrue(True)
verify.IsTrue(True, "IsTrue(True) should succeed")
verify.IsTrue(True, "IsTrue({0}) should succeed", "True")
verify.IsTrue(False, "IsTrue(False) should fail")

verify.False(False)
verify.False(False, "False(False) should succeed")
verify.False(False, "False({0}) should succeed", "False")
verify.False(True, "False(True) should fail")

verify.IsFalse(False)
verify.IsFalse(False, "IsFalse(False) should succeed")
verify.IsFalse(False, "IsFalse({0}) should succeed", "False")
verify.IsFalse(True, "IsFalse(True) should fail")

# bool
verify.AreEqual(True, True)
verify.AreEqual(True, True, "AreEqual(True, True) should succeed")
verify.AreEqual(True, True, "AreEqual({0}) should succeed", "True, True")
verify.AreEqual(True, False, "AreEqual(True, False) should fail")

# float -> passed to .NET as Double
verify.AreEqual(0.0, 0.0)
verify.AreEqual(0.0, 0.0, "AreEqual(0.0, 0.0) should succeed")
verify.AreEqual(0.0, 0.0, "AreEqual({0}) should succeed", "0.0, 0.0")
verify.AreEqual(0.0, 0.1, "AreEqual(0.0, 0.1) should fail")

None # script return